package com.platform.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description: ServiceResponse
 * @date: 2019/10/12
 * @author: gzg
 */
@Data
@ApiModel(description = "返回响应数据")
@NoArgsConstructor
public class ServiceResponse<T> implements Serializable {
    public static final int DEFAULT_CODE = 0;
    public static final String DEFAULT_MSG = "执行成功";

    @ApiModelProperty(value = "错误编码")
    private int errno;
    @ApiModelProperty(value = "错误描述")
    private String errmsg;
    @ApiModelProperty(value = "返回模块属性")
    private T data;

    public static <T> ServiceResponse generateSuccessResult(T data) {
        ServiceResponse serviceResponse = new ServiceResponse(data);
        serviceResponse.setErrno(DEFAULT_CODE);
        serviceResponse.setErrmsg(DEFAULT_MSG);
        return serviceResponse;
    }

    public static <T> ServiceResponse generateFailureResult(int errno, String errmsg) {
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.setErrno(errno);
        serviceResponse.setErrmsg(errmsg);
        return serviceResponse;
    }

    public ServiceResponse(T data) {
        this.data = data;
    }
}
